package persist

import (
	"log"
	"github.com/olivere/elastic"
	"context"
	"imooc.com/crawler/engine"
	"github.com/pkg/errors"
)

func ItemSaver(index string) (chan engine.Item, error){
	//开始连接，默认访问 http://127.0.0.1:9200
	//sniff: false，表示关闭集群;默认是开启的
	client, err := elastic.NewClient(
		elastic.SetSniff(false),
		elastic.SetURL("http://192.168.33.10:9200"),
	)
	if err != nil {
		return nil, err
	}
	out := make(chan engine.Item)
	go func() {
		itemCount := 0
		for  {
			item := <- out
			log.Printf("Item Saver: go item #%d: %v", itemCount, item)
			itemCount++

			err := Save(client, index,  item)
			if err != nil {
				log.Printf("Item Saver Error: saving item %v:%v",
					item, err)
			}
		}
	}()
	return out, nil
}

func Save(client *elastic.Client, index string,
	item engine.Item) error  {

	if item.Type == "" {
		return errors.New("must supply Type")
	}
	indexService := client.Index().Index(index).
		Type(item.Type).BodyJson(item)
	if item.Id != ""  {
		indexService.Id(item.Id)
	}
	_, err := indexService.Do(context.Background())
	if err != nil {
		return err
	}

	return nil
}
